﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports CMISObjectModel

Public Class TypeData
   Public Const RootDocumentType As String = "cmis:document"
   Public Const Document1Type As String = "sampleDocumentType1"
   Public Const RootFolderType As String = "cmis:folder"
   Public Const Folder1Type As String = "sampleFolderType1"
   Public Const RootRelationshipType As String = "cmis:relationship"
   Public Const Relationship1Type As String = "sampleRelationship1"
   Public Const RootPolicyType As String = "cmis:policy"
   Public Property Definition As TypeDefinitions.TypeDefinition
   Public Property CreationDate As DateTime = Now
   Public Property CreatedBy As String = "test"
   Private _type As TypeEnum
   Public ReadOnly Property Type As TypeEnum
      Get
         Return _type
      End Get
   End Property
   Private _baseType As BaseTypeEnum
   Public ReadOnly Property BaseType As BaseTypeEnum
      Get
         Return _baseType
      End Get
   End Property
   Public ReadOnly Property Author As String
      Get
         Return CreatedBy & "@example.org"
      End Get
   End Property
   Public Property Children As Dictionary(Of String, TypeData)
   Public Property IsBaseType As Boolean = False
   Public Enum BaseTypeEnum
      RootDocument
      RootFolder
      RootRelationship
      RootPolicy
   End Enum
   Public Enum TypeEnum
      RootDocument
      Document1
      RootFolder
      Folder1
      RootRelationship
      Relationship1
      RootPolicy
   End Enum
   Public Sub New(type As TypeEnum, children As Dictionary(Of String, TypeData))
      _type = type
      Select Case type
         Case TypeEnum.Document1, TypeEnum.RootDocument
            _baseType = BaseTypeEnum.RootDocument
         Case TypeEnum.Folder1, TypeEnum.RootFolder
            _baseType = BaseTypeEnum.RootFolder
         Case TypeEnum.RootPolicy
            _baseType = BaseTypeEnum.RootPolicy
         Case TypeEnum.RootRelationship, TypeEnum.Relationship1
            _baseType = BaseTypeEnum.RootRelationship
      End Select

      AddHandler FindParentEvent, AddressOf FindParentHandler
      Select Case type
         Case TypeEnum.RootDocument

            Dim properties As List(Of PropertyDefinitions.IPropertyDefinition) = TypeDefinitions.TypeDocumentDefinition.GetDefaultProperties(Provider.ConstNamespace, True)
            Dim retVal As New TypeDefinitions.TypeDocumentDefinition(RootDocumentType, RootDocumentType, "root document", RootDocumentType, properties, "") With {.DisplayName = RootDocumentType, .Description = RootDocumentType, .Creatable = False}
            retVal.ContentStreamAllowed = TypeDefinitions.TypeDocumentDefinition.ContentStreamAllowedEnum.Allowed

            Me.Definition = retVal
            Me.IsBaseType = True
            Me.Children = children
         Case TypeEnum.Document1

            Dim properties As List(Of PropertyDefinitions.IPropertyDefinition) = TypeDefinitions.TypeDocumentDefinition.GetDefaultProperties(Provider.ConstNamespace, False)
            Dim prop1 As New PropertyDefinitions.PropertyStringDefinition("Value1", "Value1", Provider.ConstNamespace, "Value1", "Value1", False, False, False, False, Enums.Cardinality.Single, Enums.Updatability.ReadWrite) With {.Description = "Value1"}
            Dim choices1 As New List(Of PropertyDefinitions.Choices.CmisChoice(Of String))
            choices1.Add(New PropertyDefinitions.Choices.CmisChoice(Of String) With {.DisplayName = "This is Value A1", .Values = New String() {"A1"}})
            choices1.Add(New PropertyDefinitions.Choices.CmisChoice(Of String) With {.DisplayName = "This is Value A2", .Values = New String() {"A2"}})
            choices1.Add(New PropertyDefinitions.Choices.CmisChoice(Of String) With {.DisplayName = "This is Value B1", .Values = New String() {"B1"}})
            prop1.Choices = choices1.ToArray
            properties.Add(prop1)
            properties.Add(New PropertyDefinitions.PropertyStringDefinition("Value2", "Value2", Provider.ConstNamespace, "Value2", "Value2", False, False, False, False, Enums.Cardinality.Single, Enums.Updatability.ReadWrite) With {.Description = "Value2"})
            properties.Add(New PropertyDefinitions.PropertyDecimalDefinition("Value3", "Value3", Provider.ConstNamespace, "Value3", "Value3", False, False, False, False, Enums.Cardinality.Single, Enums.Updatability.ReadWrite) With {.Description = "Value3", .MinValue = -50, .MaxValue = 50})
            Dim retVal As New TypeDefinitions.TypeDocumentDefinition(Document1Type, Document1Type, "document1", Document1Type, properties, RootDocumentType) With {.DisplayName = Document1Type, .Description = Document1Type, .Creatable = True, .Fileable = True}
            retVal.ContentStreamAllowed = TypeDefinitions.TypeDocumentDefinition.ContentStreamAllowedEnum.Allowed

            Me.Definition = retVal
            Me.IsBaseType = False
            Me.Children = children
         Case TypeEnum.RootFolder
            Dim properties As List(Of PropertyDefinitions.IPropertyDefinition) = TypeDefinitions.TypeFolderDefinition.GetDefaultProperties(Provider.ConstNamespace, True)
            Dim retVal As New TypeDefinitions.TypeFolderDefinition(RootFolderType, RootFolderType, "root folder", RootFolderType, properties, "") With {.DisplayName = RootFolderType, .Description = RootFolderType, .Creatable = False}

            Me.Definition = retVal
            Me.IsBaseType = True
            Me.Children = children
         Case TypeEnum.Folder1
            Dim properties As List(Of PropertyDefinitions.IPropertyDefinition) = TypeDefinitions.TypeFolderDefinition.GetDefaultProperties(Provider.ConstNamespace, False)
            Dim retVal As New TypeDefinitions.TypeFolderDefinition(Folder1Type, Folder1Type, "folder1", Folder1Type, properties, RootFolderType) With {.DisplayName = Folder1Type, .Description = Folder1Type, .Creatable = True}

            Me.Definition = retVal
            Me.IsBaseType = False
            Me.Children = children
         Case TypeEnum.RootRelationship
            Dim properties As List(Of PropertyDefinitions.IPropertyDefinition) = TypeDefinitions.TypeRelationshipDefinition.GetDefaultProperties(Provider.ConstNamespace, True)
            Dim retVal As New TypeDefinitions.TypeRelationshipDefinition(RootRelationshipType, RootRelationshipType, "root relationship", RootRelationshipType, properties, "") With {.DisplayName = RootRelationshipType, .Description = RootRelationshipType, .Creatable = False}

            Me.Definition = retVal
            Me.IsBaseType = True
            Me.Children = children
         Case TypeEnum.Relationship1
            Dim properties As List(Of PropertyDefinitions.IPropertyDefinition) = TypeDefinitions.TypeRelationshipDefinition.GetDefaultProperties(Provider.ConstNamespace, False)
            Dim retVal As New TypeDefinitions.TypeRelationshipDefinition(Relationship1Type, Relationship1Type, "relationship1", Relationship1Type, properties, RootRelationshipType) With {.DisplayName = Relationship1Type, .Description = Relationship1Type, .Creatable = True}

            Me.Definition = retVal
            Me.IsBaseType = False
            Me.Children = children
         Case TypeEnum.RootPolicy
            Dim properties As List(Of PropertyDefinitions.IPropertyDefinition) = TypeDefinitions.TypePolicyDefinition.GetDefaultProperties(Provider.ConstNamespace, True)
            Dim retVal As New TypeDefinitions.TypePolicyDefinition(RootPolicyType, RootPolicyType, "root policy", RootPolicyType, properties, "") With {.DisplayName = RootPolicyType, .Description = RootPolicyType, .Creatable = False}

            Me.Definition = retVal
            Me.IsBaseType = True
            Me.Children = children

      End Select


   End Sub
   Public Overrides Function ToString() As String
      Return Me.Definition.Id & " - " & MyBase.ToString()
   End Function

   Private Shared Event FindParentEvent(sender As Object, e As FindParentEventArgs(Of TypeData))
   Public Shared Function FindParent(pkey As String) As List(Of TypeData)
      Dim tmpArgs As New FindParentEventArgs(Of TypeData)(pkey)
      RaiseEvent FindParentEvent(Nothing, tmpArgs)
      Return tmpArgs.ResultData
   End Function



   Private Sub FindParentHandler(sender As Object, e As FindParentEventArgs(Of TypeData))
      If Children.ContainsKey(e.SearchPkey) Then
         e.ResultData.Add(Me)
      End If
   End Sub
End Class
